import Mock from 'mockjs'
import { Result } from './model'

import { adminMenuList, userMenuList, managerMenuList } from './config'

// 登录
Mock.mock('http://www.demo.com/login', 'post', (options: any): Result => {
  // console.log('body', body)
  const { username, password } = JSON.parse(options.body)
  if (username === 'admin' && password === '123456') {
    return {
      code: 200,
      message: 'success',
      data: {
        token: 'admin-jdhaaxcakjh1231j12398asdlzxclkjahdj%%#$&@%&#(@#9asdad',
        userName: 'admin',
        nickName: '管理员'
      }
    }
  } else if (username === 'user' && password === '123456') {
    return {
      code: 200,
      message: 'success',
      data: {
        token: `user-jdhaaxcakjh1231j12398asdlzxclkjahdj%%#$&@%&#(@#9asdad)`,
        userName: 'user',
        nickName: '普通用户'
      }
    }
  } else if (username === 'manager' && password === '123456') {
    return {
      code: 200,
      message: 'success',
      data: {
        token: `manager-jdhaaxcakjh1231j12398asdlzxclkjahdj%%#$&@%&#(@#9asdad)`,
        userName: 'manager',
        nickName: '主管'
      }
    }
  } else {
    return {
      code: 401,
      message: 'success',
      data: null
    }
  }
})

// 获取菜单
Mock.mock('http://www.demo.com/menuList', 'post', (options: any) => {
  const { token } = JSON.parse(options.body)
  if (!token) {
    return {
      code: 401,
      message: '请先登录',
      data: null
    }
  }
  if (token.indexOf('admin') > -1) {
    return {
      code: 200,
      message: 'success',
      data: {
        menuList: adminMenuList
      }
    }
  }
  if (token.indexOf('user') > -1) {
    return {
      code: 200,
      message: 'success',
      data: {
        menuList: userMenuList
      }
    }
  }
  if (token.indexOf('manager') > -1) {
    return {
      code: 200,
      message: 'success',
      data: {
        menuList: managerMenuList
      }
    }
  }
})
